# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import pymysql as pymysql
from redis import Redis

# class FirstbloodPipeline:
#     def process_item(self, item, spider):
#         print(item)
#         return item

from scrapy.pipelines.images import ImagesPipeline
import scrapy
class ImgsPipelin(ImagesPipeline):
    # 根据图片地址发起请求
    def get_media_requests(self, item, info):
        yield scrapy.Request(item['src'], meta={'item': item})
    # 返回图片名称
    def file_path(self, request, response=None, info=None, *, item=None):
        item = request.meta['item']
        filePath = item['name']
        return filePath
    # 将item传递给下一个即将被执行的管道类
    def item_completed(self, results, item, info):
        return item

# # 将数据写入mysql
# class MysqlPipeline():
#     conn = None
#     cursor = None
#
#     def open_spider(self, spider):
#         self.conn = pymysql.Connect(host='127.0.0.1', port=3306, user='root', password='123456',
#                                     db='spider', charset='utf8')
#         print(self.conn)
#
#     def process_item(self, item, spider):
#         self.cursor = self.conn.cursor()
#         sql = 'insert into duanziwang values("%s", "%s")' % (item['title'], item['content'])
#
#         # 事务处理
#         try:
#             self.cursor.execute(sql)
#             self.conn.commit()
#         except Exception as e:
#             print(e)
#             self.conn.rollback()
#         return item  # 注意return item，继续传递给下一个redis管道类使用
#
#     def close_spider(self, spider):
#         self.cursor.close()
#         self.conn.close()
#
# # 将数据写入redis
# class RedisPipeline():
#     conn = None
#     def open_spider(self,spider):
#         self.conn = Redis(host='127.0.0.1', port=6379)
#         print(self.conn)
#
#     def process_item(self, item, spider):
#         dict_content = {
#             'title': item['title'],
#             'content': item['content']
#         }
#         self.conn.lpush('duanziData', str(dict_content))    # 第三方库redis升级到3.0后仅接受用户数据为字节、字符串或数字（整数，长整数和浮点数）
#         return item
